//
//  AreaPickerView.swift
//  zhendeBusiness
//
//  Created by 陆文宇 on 2017/5/11.
//  Copyright © 2017年 chenliang. All rights reserved.
//

import UIKit
import RealmSwift

class YYAreaPickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
    
    @IBOutlet weak var bView: UIView!
    @IBOutlet weak var pickerView: UIPickerView!
    
    var doneClosure: ((YYCity) -> Void)!
    var cities0 = [YYCity]()
    var cities1 = [YYCity]()
    var cities2 = [YYCity]()
    
    static func show(completionHandler: @escaping (YYCity) -> Void) {
        let areaV = Bundle.main.loadNibNamed("YYAreaPickerView", owner: nil, options: nil)?.first as! YYAreaPickerView
        areaV.frame = UIScreen.main.bounds
        UIApplication.shared.keyWindow?.addSubview(areaV)
        UIView.animate(withDuration: 0.3) { 
            areaV.bView.transform = CGAffineTransform(translationX: 0, y: -(screenWidth * 0.65 + 40))
        }
        
        areaV.doneClosure = completionHandler
        areaV.getData()
    }
    
    func getData() {
        let cities = YYCity.getSubAll(id: -1)
        cities0 = cities
        if !cities.isEmpty {
            if let id = cities0.first?.id {
                cities1 = YYCity.getSubAll(id: id)
            }
            if let id = cities1.first?.id {
                cities2 = YYCity.getSubAll(id: id)
            }
            
            pickerView.dataSource = self
            pickerView.delegate = self
            pickerView.reloadAllComponents()
        }
    }
    
    func getCities(component: Int) -> [YYCity] {
        switch component {
        case 0:
            return cities0
        case 1:
            return cities1
        case 2:
            return cities2
        default:
            return []
        }
    }
    
    @IBAction func tapCancel(_ sender: UITapGestureRecognizer) {
        cancel(sender)
    }
    
    @IBAction func cancel(_ sender: Any) {
        UIView.animate(withDuration: 0.3, animations: { 
            self.bView.transform = CGAffineTransform.identity
        }) { (finish) in
            if finish {
                self.removeFromSuperview()
            }
        }
    }
    
    @IBAction func done(_ sender: Any) {
        var city: YYCity
        if !cities2.isEmpty {
            city = cities2[pickerView.selectedRow(inComponent: 2)]
        } else if !cities1.isEmpty {
            city = cities1[pickerView.selectedRow(inComponent: 1)]
        } else {
            city = cities0[pickerView.selectedRow(inComponent: 0)]
        }
        
        doneClosure(city)
        cancel(sender)
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return getCities(component: component).count
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        if let view = view {
            (view as! UILabel).text = getCities(component: component)[row].name
            return view
        } else {
            for v in pickerView.subviews {
                if v.yy_height < 1 {
                    v.backgroundColor = .darkGray
                }
            }
            let view = UILabel()
            view.textAlignment = .center
            view.font = UIFont.systemFont(ofSize: 13)
            view.text = getCities(component: component)[row].name
            return view
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            cities1 = YYCity.getSubAll(id: cities0[row].id)
            if let id = cities1.first?.id {
                cities2 = YYCity.getSubAll(id: id)
            } else {
                cities2.removeAll()
            }
            pickerView.reloadComponent(1)
            pickerView.reloadComponent(2)
        } else if component == 1 {
            cities2 = YYCity.getSubAll(id: cities1[row].id)
            pickerView.reloadComponent(2)
        }
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
